//
//  NSAttributedString+BtnAddtion.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/4/1.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "NSAttributedString+BtnAddtion.h"
#import <UIKit/UIKit.h>
//#import <AppKit/AppKit.h>


@implementation NSAttributedString (BtnAddtion)

+ (instancetype)creatBtnWithImage:(UIImage *)image withImageWight:(CGFloat)wight withImageHeight:(CGFloat)height withTitle:(NSString *)title wihtFontSize:(CGFloat)fontSize withtitleColor:(UIColor *)color spacing:(CGFloat)spacing{
    //文字字典
    NSDictionary *titleDic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName:color};
    //文字与图片之间的间隔
    NSDictionary *spaceDic = @{NSFontAttributeName:[UIFont systemFontOfSize:spacing]};
    
    //图片文本
    NSTextAttachment *attachment = [[NSTextAttachment alloc]init];
    attachment.image = image;
    //设置图片的位置与大小
    attachment.bounds = CGRectMake(0, 0, wight, height);
    NSAttributedString *imageText = [NSAttributedString attributedStringWithAttachment:attachment];
    
    //文字换行
    NSAttributedString *textChange = [[NSAttributedString alloc]initWithString:@"\n\n" attributes:spaceDic];
    
    //按钮文字
    NSAttributedString *btnTitle = [[NSAttributedString alloc]initWithString:title attributes:titleDic];
    
    //合并文字
    NSMutableAttributedString *attM = [[NSMutableAttributedString alloc]initWithAttributedString:imageText];
    [attM appendAttributedString:textChange];
    [attM appendAttributedString:btnTitle];
    
    return attM.copy;
}

@end
